package com.jic.manager.controller.rest.sys;

import com.alibaba.fastjson.JSONObject;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.manager.api.rest.UserSysDeptApi;
import com.jic.manager.common.util.BeanUtil;
import com.jic.manager.entity.SysDept;
import com.jic.manager.mapper.SysDeptMapper;
import com.jic.manager.request.MestorePageRequest;
import com.jic.manager.request.UccUserRequest;
import com.jic.manager.response.MestoreResponse;
import com.jic.manager.response.SysDeptResponse;
import com.jic.manager.response.UccUserResponse;
import com.jic.manager.service.MestoreService;
import com.jic.manager.service.SysDeptService;
import com.jic.manager.utils.UccHttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * description: UserSysDeptApiController
 * date: 2020/7/22 13:45
 * author: wangys
 * version: 1.0
 */
@RestController
@RequestMapping("userSysDept")
@Slf4j
public class UserSysDeptApiController implements UserSysDeptApi {
    @Autowired
    SysDeptService sysDeptService;

    @Autowired
    SysDeptMapper sysDeptMapper;

    @Resource
    private UccHttpUtils uccHttpUtils;

    @Resource
    private MestoreService mestoreService;


    @Override
    @PostMapping("getUserProviderAuthorDept")
    public RestResult<List<SysDeptResponse>> getUserProviderAuthorDept(@RequestParam("userId")Long userId, @RequestParam("deptId")Long deptId) {
        return RestResult.success(sysDeptService.getUserProviderAuthorDept(userId, deptId));
    }

    @Override
    @PostMapping("queryDeptNameById")
    public RestResult<String> queryDeptNameById(@RequestParam("deptId")Long deptId) {
        return sysDeptService.queryDeptNameById(deptId);
    }

    @Override
    @PostMapping("getDeptList")
    public List<SysDeptResponse> getDeptList() {
        List<SysDeptResponse> sysDeptList = new ArrayList<>();
        try {
            SysDept sysDept = new SysDept();
            sysDept.setPid(Long.valueOf(0));
            List<SysDept> list = sysDeptMapper.query(sysDept);
            if (CollectionUtils.isNotEmpty(list)) {
                list.forEach(item -> {
                    SysDeptResponse sysDeptResponse = new SysDeptResponse();
                    BeanUtil.copy(item, sysDeptResponse);
                    sysDeptList.add(sysDeptResponse);
                    SysDept sysDept1 = new SysDept();
                    sysDept1.setPid(item.getId());
                    List<SysDept> list1 = sysDeptMapper.query(sysDept1);
                    if (CollectionUtils.isNotEmpty(list1)) {
                        list1.forEach(item1 -> {
                            SysDeptResponse sysDeptResponse1 = new SysDeptResponse();
                            BeanUtil.copy(item1, sysDeptResponse1);
                            sysDeptList.add(sysDeptResponse1);
                        });
                    }
                });
            }
        } catch (Exception e) {
            log.error("查询母平台和所有子平台失败==>>>{}", e);
        }
        return sysDeptList;
    }

    @Override
    @PostMapping("getAllDeptList")
    public List<SysDeptResponse> getAllDeptList() {
        List<SysDeptResponse> sysDeptList = new ArrayList<>();
        try {
            SysDept sysDept = new SysDept();
            List<SysDept> list = sysDeptMapper.query(sysDept);
            if (CollectionUtils.isNotEmpty(list)) {
                list.forEach(item -> {
                    SysDeptResponse sysDeptResponse = new SysDeptResponse();
                    BeanUtil.copy(item, sysDeptResponse);
                    sysDeptList.add(sysDeptResponse);
                });
            }
        } catch (Exception e) {
            log.error("查询所有平台失败==>>>{}", e);
        }
        return sysDeptList;
    }

    @Override
    @PostMapping("getDeptIdByBusinessId")
    public List<SysDeptResponse> getDeptIdByBusinessId(HashMap map) {
        List<SysDeptResponse> sysDeptList = new ArrayList<>();
        try {
            SysDept sysDept = new SysDept();
            sysDept.setBusinessId(Long.valueOf(map.get("businessId").toString()));
            sysDept.setDeptType(Integer.valueOf(map.get("deptType").toString()));
            List<SysDept> list = sysDeptMapper.query(sysDept);
            if (CollectionUtils.isNotEmpty(list)) {
                list.forEach(item -> {
                    SysDeptResponse sysDeptResponse = new SysDeptResponse();
                    BeanUtil.copy(item, sysDeptResponse);
                    sysDeptList.add(sysDeptResponse);
                });
            }
        } catch (Exception e) {
            log.error("根据业务id 获取部门id ==>>>{}", e);
        }
        return sysDeptList;
    }

    @Override
    @PostMapping("getUccUserListByPhone")
    public List<UccUserResponse> getUccUserListByPhone(UccUserRequest uccUserRequest) {
        //List<UccUserResponse> uccUserListByPhone = uccHttpUtils.getUccUserListByPhone(uccUserRequest);
        return null;
    }

    @Override
    @PostMapping("queryListByIds")
    public List<SysDeptResponse> queryListByIds(@RequestParam(value ="deptIdList" ) List<Long> deptIdList) {
        List<SysDeptResponse> sysDeptList = new ArrayList<>();
        try {
            String ids = StringUtils.join(deptIdList,",");
            List<SysDept> list = sysDeptMapper.queryListByIds(ids);
            if (CollectionUtils.isNotEmpty(list)) {
                list.forEach(item -> {
                    SysDeptResponse sysDeptResponse = new SysDeptResponse();
                    BeanUtil.copy(item, sysDeptResponse);
                    sysDeptList.add(sysDeptResponse);
                });
            }
        } catch (Exception e) {
            log.error("查询所有平台失败==>>>{}", e);
        }
        return sysDeptList;
    }


    @Override
    @PostMapping("queryUserInfoByAccessToken")
    public UccUserResponse queryUserInfoByAccessToken(String accessToken) {
        UccUserResponse uccUserOutputDTO = null;
        try {
            String userInfoByAccessToken = "";//uccHttpUtils.getUserInfoByAccessToken(accessToken);
            uccUserOutputDTO = JSONObject.parseObject(userInfoByAccessToken, UccUserResponse.class);
        } catch (Exception e) {
            log.error("queryUserInfoByAccessToken error, e = , accessToken = {}", e, accessToken);
        }
        return uccUserOutputDTO;
    }

    @Override
    @PostMapping("getStorePage")
    public RestResult<PageResult<MestoreResponse>> getStorePage(MestorePageRequest mestorePageRequest) {
        return mestoreService.getStorePage(mestorePageRequest);
    }
}
